"""
行政区划 | 映射
"""

import utils.enum_utils as enum
from mapping.base_mapping import MappingBase
from models.po import (
    RegionAreaPO,
    RegionCityPO,
    RegionProvincePO,
    RegionStreetPO,
)
from models.vo import (
    RegionDetailVO,
    RegionListPage,
    RegionListVO,
)
from my_typing import *

UnionPO = RegionProvincePO | RegionCityPO | RegionAreaPO | RegionStreetPO


class RegionMapping(MappingBase):
    """ 行政区划 | 映射 """

    id_field = "code"
    po = UnionPO
    list_vo = RegionListVO
    detail_vo = RegionDetailVO
    vo_page = RegionListPage

    @classmethod
    def get_list_dic(cls, po: UnionPO) -> DictStrAny:
        """ 获取列表页字典 """

        from biz.region_biz import get_region_level_by_code

        area_level: enum.RegionLevel = get_region_level_by_code(po.code)
        base_dic: dict[str, enum.RegionLevel] = {"area_level": area_level}

        return {
            **po.model_dump(),
            **base_dic,
            **{
                field: cls.get_field_value(po, field_map)
                for field, field_map in cls.list_mapping.items()
            },
        }

    @classmethod
    def get_extend_info(cls, po: UnionPO) -> DictStrAny:
        """ 获取扩展信息 """

        from biz.region_biz import get_fullpath_code_2_name

        code_2_name: DictStrStr = get_fullpath_code_2_name(po.code)
        code_field_list: ListStr = [
            "province_code",
            "city_code",
            "area_code",
            "street_code",
        ]

        extend_dic: DictStrAny = {
            "province_code": None,
            "city_code": None,
            "area_code": None,
            "street_code": None,
            "code_list": [],
            "name_list": [],
        }

        for code_field, (code, name) in zip(code_field_list, code_2_name.items()):
            extend_dic[code_field] = code
            extend_dic["code_list"].append(code)
            extend_dic["name_list"].append(name)

        return extend_dic

    @classmethod
    def get_detail_dic(cls, po: UnionPO) -> DictStrAny:
        """ 获取详情页字典 """

        return {
            **cls.get_list_dic(po),
            **cls.get_extend_info(po),
            **{
                field: cls.get_field_value(po, field_map)
                for field, field_map in cls.detail_mapping.items()
            },
        }
